//
//  Circle.m
//  Project5
//
//  Created by Nguyen Tan Loc on 5/18/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "Circle.h"

@implementation Circle
@synthesize _isDestroy = m_IsDestroy, _CIID = m_CIID;

-(id) initWithRadius:(float)r angle:(float)a
{
    if (self = [super init])
    {
        m_Radius = r;
        m_ArrRound = [[NSMutableArray alloc] init];
        m_Angle = a;
        m_Speed = 0;
        m_Position = ccp(m_Radius * sinf(CC_DEGREES_TO_RADIANS(m_Angle)), m_Radius * cosf(CC_DEGREES_TO_RADIANS(m_Angle)));
    }
    return self;
}

-(void) appear
{
    for (int i = 0; i < m_ArrRound.count; i++)
    {
        CircleRound *r = [m_ArrRound objectAtIndex:i];
        [r appearWithTime:(1.0f * (1 - (float)i / m_ArrRound.count))];
    }
}

-(void) addRoundWithRadius:(float)r rotationSpeed:(float)s listSegment:(NSArray *)listSegment
{
    CircleRound *round = [[CircleRound alloc] initWithRadius:r listSegment:listSegment];
    [round setPosition:m_Position];
    [round setRotationSpeed:s];
    [m_ArrRound addObject:round];
    [round release];
}

-(void) addSpriteToBatch:(CCSpriteBatchNode *)batch
{
    for (int i = 0; i < m_ArrRound.count; i++)
    {
        CircleRound *r = [m_ArrRound objectAtIndex:i];
        [r addSpriteToBatch:batch];
    }
}

-(void) setPosition:(CGPoint)p
{
    m_Position = p;
    for (int i = 0; i < m_ArrRound.count; i++)
    {
        CircleRound *r = [m_ArrRound objectAtIndex:i];
        [r setPosition:p];
    }
}

-(void) setRotationSpeed:(float)speed
{
    m_Speed = speed;
}

-(void) setCIID:(int)i
{
    m_CIID = i;
}

-(void) stopRotation
{
    m_Speed = 0.0f;
}


-(void) update:(ccTime)t
{
    m_Angle += m_Speed * t * [LevelConfig sharedLevelConfig]._gSpeed;
    [self setPosition:ccp(m_Radius * sinf(CC_DEGREES_TO_RADIANS(m_Angle)), m_Radius * cosf(CC_DEGREES_TO_RADIANS(m_Angle)))];
}


-(BOOL) checkCollideWithBullet:(Bullet *)b
{
    float distance = ccpLength(ccp(b.position.x - m_Position.x ,b.position.y - m_Position.y)) - b._radius;
//    float angle = atan2f(b.position.x - m_Position.x, b.position.y - m_Position.y) * 180.0f / kmPI;
//    if (angle < 0.0f)
//        angle += 360.0f;
    for (int i = 0; i < m_ArrRound.count; i++)
    {
        CircleRound *r = [m_ArrRound objectAtIndex:i];
        
        if (r._isDestroy) continue;
        
        if ([r isCollideBulletWithDistance:distance bullet:b])
        {
            [self checkSpecialBulletWithType:b._type index:i];
            [b explode];
            
            [self checkCircleDestroy];
            return YES;
        }
        else
        {
            
        }
    }
    
    return NO;
}

-(void) checkSpecialBulletWithType:(int)t index:(int)i
{

    if (t == BULLET_TYPE_POWER_2)
    {
        int endRound = (i + 3) < m_ArrRound.count ? (i + 3) : m_ArrRound.count;
        for (int j = i + 1; j < endRound; j++)
        {
            CircleRound *r = [m_ArrRound objectAtIndex:j];
            [r destroyWithDelay:((j - i) * 0.05f)];
        }
    }
    else if (t == BULLET_TYPE_POWER_FULL)
    {
        for (int j = i + 1; j < m_ArrRound.count; j++)
        {
            CircleRound *r = [m_ArrRound objectAtIndex:j];
            [r destroyWithDelay:((j - i) * 0.05f)];
        }
    }
}

-(void) checkCircleDestroy
{
    for (int i = 0; i < m_ArrRound.count; i++)
    {
        CircleRound *r = [m_ArrRound objectAtIndex:i];
        if (!r._isDestroy) return;
    }
    m_IsDestroy = YES;
}


-(void) dealloc
{
    [super dealloc];
    
    [m_ArrRound release];
    m_ArrRound = nil;
}

@end
